using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Web.UI;
using System.Web.UI.WebControls;
using Meditek.UI.Controls;
using Meditek.UI.PersonInmunizationsReference;
using Meditek.Entities;
using Meditek.UI.InmunizationsReference;
using Meditek.UI.PersonsReference;

namespace Meditek.UI
{
    public partial class PersonInmunizationsForm : System.Web.UI.Page
    {
		
		#region Private Variables
		
		private PersonInmunizationsServiceClient _personInmunizationsServiceClient = null;
		
		#endregion

		#region Page Events
  
		protected void Page_Load(object sender, EventArgs e)
        {
			if (!Page.IsPostBack)
            {
                if (Request.QueryString["Mode"].ToString() == "Edit")
                {
                    (this.Master.FindControl("Logo").FindControl("lblFormTitle") as Label).Text = "Edit PersonInmunizations";
                    EditFormView.DefaultMode = FormViewMode.Edit;
                    (EditFormView.FindControl("btnSave") as Button).Visible = false;
                }
                else
                {
                    (this.Master.FindControl("Logo").FindControl("lblFormTitle") as Label).Text = "New PersonInmunizations";
                    EditFormView.DefaultMode = FormViewMode.Insert;
                    (EditFormView.FindControl("btnUpdate") as Button).Visible = false;
                }
			}
		}
		
        protected void PersonInmunizationsDataSource_CommitChanges(object sender, ObjectDataSourceStatusEventArgs e)
        {
			try
            {
                _personInmunizationsServiceClient.CommitChanges();
                (this.Master.FindControl("Information").FindControl("msglblError") as MessageLabel).ShowInfo("Item Saved Successfully!", false);
                Response.Redirect("PersonInmunizationsList.aspx");
            }
            catch (Exception ex)
            {
				(this.Master.FindControl("Information").FindControl("msglblError") as MessageLabel).ShowInfo(ex.Message, true);
            }
        }
        
        protected void PersonInmunizationsDataSource_ObjectCreated(object sender, ObjectDataSourceEventArgs e)
        {
            _personInmunizationsServiceClient = e.ObjectInstance as PersonInmunizationsServiceClient;
        }

		protected void EditFormView_ItemInserting(object sender, FormViewInsertEventArgs e)
        {
            HandleEmptyValues(e.Values);
        }

        protected void EditFormView_ItemUpdating(object sender, FormViewUpdateEventArgs e)
        {
            HandleEmptyValues(e.NewValues);
        }
			
		
		protected void drlstidInmunization_DataBinding(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
                PrepareidInmunization();
        }
			
		
		protected void drlstPerson_DataBinding(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
                PreparePerson();
        }
 
		#endregion

		#region Helper Functions

		private void HandleEmptyValues(IOrderedDictionary EntryValues)
        {
            int Index = 0;
            foreach (DictionaryEntry entry in EntryValues)
            {
                if (entry.Value.ToString() == string.Empty)
                    EntryValues[Index] = null;
                Index++;
            }
        }
			
        
        private void PrepareidInmunization()
        {
			InmunizationsServiceClient inmunizationsServiceClient = new InmunizationsServiceClient();
			List<Inmunizations> inmunizationsList = inmunizationsServiceClient.GetAll();
			    
			(EditFormView.FindControl("drlstidInmunization") as DropDownList).DataSource = inmunizationsList;
			(EditFormView.FindControl("drlstidInmunization") as DropDownList).DataTextField = "Name";
			(EditFormView.FindControl("drlstidInmunization") as DropDownList).DataValueField = "idInmunization";
        }
			
        
        private void PreparePerson()
        {
			PersonsServiceClient personsServiceClient = new PersonsServiceClient();
			List<Persons> personsList = personsServiceClient.GetAll();
			    
			(EditFormView.FindControl("drlstPerson") as DropDownList).DataSource = personsList;
			(EditFormView.FindControl("drlstPerson") as DropDownList).DataTextField = "idPerson";
			(EditFormView.FindControl("drlstPerson") as DropDownList).DataValueField = "idPerson";
        }

		#endregion

		#region Custom Code
		
		
		#endregion

    }
}
